package br.com.wssa.smmb.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.wssa.smmb.constants.SmmbConstants;
import br.com.wssa.smmb.dto.form.ContatoForm;
import br.com.wssa.smmb.exception.BusinessException;
import br.com.wssa.smmb.exception.ServiceException;
import br.com.wssa.smmb.service.ContatoService;

/**
 * Classe ContatoController.
 * 
 * @author ssilva
 */
@Controller
public class ContatoController {

	@Autowired
	private ContatoService contatoService;

	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/contato", method = RequestMethod.POST,
		consumes = MediaType.APPLICATION_JSON_VALUE, produces = SmmbConstants.PRODUCES_JSON_VALUE)
	public ModelAndView contato(Map<String, Object> model) {
		ContatoForm contatoForm = new ContatoForm();
		model.put("contatoForm", contatoForm);
		ModelAndView modelView = new ModelAndView("/contato", model);
		return modelView;
	}

	/**
	 * dashboard.
	 * 
	 * @param model
	 *            Map<String, Object>
	 * @return String
	 */
	@RequestMapping("/enviarEmail")
	public ModelAndView smmb(ContatoForm contatoForm, Map<String, Object> model) {
		model.put("contato", contatoForm);
		ModelAndView modelView = new ModelAndView("/contato", model);
		try {
			contatoService.enviarEmail();
		} catch (BusinessException e) {
			modelView.addObject("mensagem", e.getMensagem());
		} catch (ServiceException e) {
			modelView.addObject("mensagem", e.getMensagem());
		}
		
		return modelView;
	}
}
